﻿Imports System.Text
Imports NLocalizer

Public Class PlaylistAndChannelManagement
    Dim IdPlaylistInLstChaine As Integer

#Region "Traduction"
    Private _Fichier As String = "Fichier"
    Public Property Fichier As String
        Get
            Return _Fichier
        End Get
        Set(ByVal value As String)
            _Fichier = value
        End Set
    End Property
    Private _Attention As String = "ATTENTION !"
    Public Property Attention As String
        Get
            Return _Attention
        End Get
        Set(ByVal value As String)
            _Attention = value
        End Set
    End Property
    Private _RegenererBase1 As String = "Cette commande vide toutes vos listes de chaînes et toutes vos chaînes."
    Public Property RegenererBase1 As String
        Get
            Return _RegenererBase1
        End Get
        Set(ByVal value As String)
            _RegenererBase1 = value
        End Set
    End Property

    Private _RegenererBase2 As String = "Régénérer la base de données"
    Public Property RegenererBase2 As String
        Get
            Return _RegenererBase2
        End Get
        Set(ByVal value As String)
            _RegenererBase2 = value
        End Set
    End Property

    Private _SupprimerChaine1 As String = "Cette commande supprime la chaîne."
    Public Property SupprimerChaine1 As String
        Get
            Return _SupprimerChaine1
        End Get
        Set(ByVal value As String)
            _SupprimerChaine1 = value
        End Set
    End Property
    Private _SupprimerChaine2 As String = "Supprimer chaîne"
    Public Property SupprimerChaine2 As String
        Get
            Return _SupprimerChaine2
        End Get
        Set(ByVal value As String)
            _SupprimerChaine2 = value
        End Set
    End Property

#End Region

#Region "Base de données"
    Private Sub btRecreerBDD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btRecreerBDD.Click

        Dim rep As DialogResult = MessageBox.Show(_Attention & Environment.NewLine & Environment.NewLine & _RegenererBase1, _RegenererBase2, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
        If rep = DialogResult.OK Then
            DeleteDataBase()
            CreateDataBase()
        End If
    End Sub

    Private Sub btImportXSPF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btImportXSPF.Click
        ofdPlaylist.FileName = ""
        ofdPlaylist.InitialDirectory = "c:\"
        ofdPlaylist.Filter = _Fichier & " XSPF(*.xspf)|*.xspf"
        If ofdPlaylist.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            ImportXSPF(ofdPlaylist.FileName, ofdPlaylist.SafeFileName)
            rafraichirplaylist()
        End If
    End Sub

    Private Sub btImportM3U_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btImportM3U.Click
        ofdPlaylist.FileName = ""
        ofdPlaylist.InitialDirectory = "c:\"
        ofdPlaylist.Filter = _Fichier & " m3u(*.m3u)|*.m3u"
        If ofdPlaylist.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            ImportM3U(ofdPlaylist.FileName, ofdPlaylist.SafeFileName)
            rafraichirplaylist()
        End If
    End Sub

    Private Sub btExportSqlite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btExportSqlite.Click
        If lvwPlaylist.SelectedItems.Count = 1 Then
            SaveFileDialog1.FileName = lvwPlaylist.SelectedItems(0).Text
            SaveFileDialog1.InitialDirectory = "c:\"
            SaveFileDialog1.Filter = _Fichier & " zv(*.zv)|*.zv"
            If SaveFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
                ExportSQLite(SaveFileDialog1.FileName, lvwPlaylist.SelectedItems(0).SubItems(1).Text)
            End If
        End If
    End Sub

    Private Sub btImportSQLite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btImportSQLite.Click
        ofdPlaylist.FileName = ""
        ofdPlaylist.InitialDirectory = "c:\"
        ofdPlaylist.Filter = _Fichier & " zv(*.zv)|*.zv"
        If ofdPlaylist.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            ImportSQLite(ofdPlaylist.FileName)
            rafraichirplaylist()
        End If

    End Sub

    Private Sub btImportPtvMod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btImportPtvMod.Click
        ofdPlaylist.FileName = "chaines.xml"
        ofdPlaylist.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & "\Pouchin TV Mod2\"
        ofdPlaylist.Filter = _Fichier & " chaines(chaines.xml)|chaines.xml"
        If ofdPlaylist.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            ImportPTVMod(ofdPlaylist.FileName)
            rafraichirplaylist()
        End If
    End Sub

    Private Sub btImportXine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btImportXine.Click
        ofdPlaylist.FileName = ""
        ofdPlaylist.InitialDirectory = "c:\"
        ofdPlaylist.Filter = _Fichier & " conf(*.conf)|*.conf"
        If ofdPlaylist.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            ImportConf(ofdPlaylist.FileName, ofdPlaylist.SafeFileName)
            rafraichirplaylist()
        End If

    End Sub

    Private Sub btImportVDR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btImportVDR.Click
        ofdPlaylist.FileName = ""
        ofdPlaylist.InitialDirectory = "c:\"
        ofdPlaylist.Filter = _Fichier & " conf(*.conf)|*.conf"
        If ofdPlaylist.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            ImportConfVDR(ofdPlaylist.FileName, ofdPlaylist.SafeFileName)
            rafraichirplaylist()
        End If

    End Sub

#End Region

#Region "Playlist"
    Private Sub rafraichirplaylist()
        lvwPlaylist.Items.Clear()
        Dim reste As Integer
        Dim playlist() As ListPlayList = LoadPlaylist()
        For i As Integer = 0 To playlist.Count - 1
            Dim lwitem As New ListViewItem(playlist(i).Name)
            lwitem.SubItems.Add(playlist(i).Id.ToString)
            Math.DivRem(i, 2, reste)
            If (reste = 0) Then
                lwitem.BackColor = Color.WhiteSmoke
            End If

            lvwPlaylist.Items.Add(lwitem)
        Next

    End Sub
    Private Sub btRafraichirPlaylist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btRafraichirPlaylist.Click
        rafraichirplaylist()
    End Sub

    Private Sub lvwPlaylist_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwPlaylist.SelectedIndexChanged
        Dim reste As Integer
        If lvwPlaylist.SelectedItems.Count = 1 Then
            IdPlaylistInLstChaine = CInt(lvwPlaylist.SelectedItems(0).SubItems(1).Text)
            'lvwChaine.Items.Clear()
            lstChaine.Items.Clear()
            Dim channelList As Channels() = LoadChannelsFromPlaylist(CLng(lvwPlaylist.SelectedItems(0).SubItems(1).Text))
            For i As Integer = 0 To channelList.Count - 1
                Dim lwitem As New ListViewItem(channelList(i).Name)
                lwitem.SubItems.Add(channelList(i).ordering.ToString)
                lwitem.SubItems.Add(channelList(i).Id.ToString)
                ' lvwChaine.Items.Add(lwitem)
                lstChaine.Items.Add(lwitem)
                Math.DivRem(i, 2, reste)
                If (reste = 0) Then
                    lwitem.BackColor = Color.WhiteSmoke
                End If
            Next
        End If
        If lstChaine.Items.Count > 0 Then
            lstChaine.Items(0).Selected = True
        End If

    End Sub

    Private Sub btListParDefaut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btListParDefaut.Click
        ActivePlaylist = CLng(lvwPlaylist.SelectedItems(0).SubItems(1).Text)
    End Sub

    Private Sub btSupprimerPlaylist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSupprimerPlaylist.Click
        If lvwPlaylist.SelectedItems.Count = 1 Then
            DeletePlaylist(CLng(lvwPlaylist.SelectedItems(0).SubItems(1).Text))
        End If
        rafraichirplaylist()
    End Sub

#End Region

#Region "Chaines"

    Private Sub btVoirChaine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btVoirChaine.Click
        If lstChaine.SelectedItems.Count = 1 Then
            Watch()
        End If
    End Sub

    Private Sub btMonter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btMonter.Click

        If lstChaine.SelectedIndices.Count = 1 AndAlso lstChaine.SelectedIndices.Item(0) > 0 Then
            Dim SelIndex As Integer = Me.lstChaine.SelectedIndices.Item(0)
            Dim tItem As ListViewItem
            tItem = lstChaine.Items(SelIndex - 1)
            lstChaine.Items(SelIndex - 1) = CType(lstChaine.Items(SelIndex).Clone, ListViewItem)
            lstChaine.Items(SelIndex - 1).SubItems(1).Text = SelIndex.ToString
            lstChaine.Items(SelIndex) = CType(tItem.Clone, ListViewItem)
            lstChaine.Items(SelIndex).SubItems(1).Text = (SelIndex + 1).ToString
            faireBGListview()
            lstChaine.Items(SelIndex - 1).Selected = True
            lstChaine.Select()
        End If

    End Sub

    Private Sub btDescendre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDescendre.Click

        If lstChaine.SelectedIndices.Count = 1 AndAlso lstChaine.SelectedIndices.Item(0) < lstChaine.Items.Count - 1 Then
            Dim SelIndex As Integer = Me.lstChaine.SelectedIndices.Item(0) + 1
            Dim tItem As ListViewItem
            tItem = lstChaine.Items(SelIndex - 1)
            lstChaine.Items(SelIndex - 1) = CType(lstChaine.Items(SelIndex).Clone, ListViewItem)
            lstChaine.Items(SelIndex - 1).SubItems(1).Text = SelIndex.ToString
            lstChaine.Items(SelIndex) = CType(tItem.Clone, ListViewItem)
            lstChaine.Items(SelIndex).SubItems(1).Text = (SelIndex + 1).ToString
            faireBGListview()
            lstChaine.Items(SelIndex).Selected = True
            lstChaine.Select()

        End If
    End Sub

    Private Sub btSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSupprimer.Click

        Dim rep As DialogResult = MessageBox.Show(_Attention & Environment.NewLine & Environment.NewLine & _SupprimerChaine1, _SupprimerChaine2, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)

        If rep = DialogResult.OK Then
            If lstChaine.SelectedIndices.Count = 1 Then
                lstChaine.Items.RemoveAt(lstChaine.SelectedIndices.Item(0))
                For i As Integer = 0 To lstChaine.Items.Count - 1
                    lstChaine.Items(i).SubItems(1).Text = (i + 1).ToString
                Next
                faireBGListview()
            End If
        End If
    End Sub

    Private Sub lstChaine_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstChaine.SelectedIndexChanged
        GroupBox4.Visible = False
        If lstChaine.SelectedItems.Count = 1 Then
            ChooseChanneById(CInt(lstChaine.SelectedItems(0).SubItems(2).Text))
        End If
    End Sub

    Sub faireBGListview()
        For r As Integer = 0 To lstChaine.Items.Count - 1
            Dim reste As Integer
            Math.DivRem(r, 2, reste)
            If (reste = 0) Then lstChaine.Items(r).BackColor = Color.WhiteSmoke Else lstChaine.Items(r).BackColor = Color.White
        Next
    End Sub

    Private Sub btEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btEnregistrer.Click
        Dim strNotIN As StringBuilder = New StringBuilder
        strNotIN.Append(" (")
        For Each listitem As ListViewItem In lstChaine.Items
            Dim ordering As Integer = CInt(listitem.SubItems(1).Text)
            Dim id As Integer = CInt(listitem.SubItems(2).Text)
            UpdateChannelOrdering(ordering, id)
            UpdateChannelName(listitem.SubItems(0).Text, id)
            strNotIN.Append("'" & id.ToString & "', ")
        Next
        strNotIN.Remove(strNotIN.Length - 2, 1).Append(")")
        DeleteChannel(strNotIN.ToString, IdPlaylistInLstChaine)
        'redemarrage du player
        My.Settings.IdChaine = 0
        If IsCreated Then
            DisposeVlcPlayer()
        End If
        initDatabase(My.Settings.IdChaine)
    End Sub

    Private Sub btAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAnnuler.Click
        Dim reste As Integer
        If lvwPlaylist.SelectedItems.Count = 1 Then
            lstChaine.Items.Clear()
            Dim channelList As Channels() = LoadChannelsFromPlaylist(CLng(lvwPlaylist.SelectedItems(0).SubItems(1).Text))
            IdPlaylistInLstChaine = CInt(lvwPlaylist.SelectedItems(0).SubItems(1).Text)
            For i As Integer = 0 To channelList.Count - 1
                Dim lwitem As New ListViewItem(channelList(i).Name)
                lwitem.SubItems.Add(channelList(i).ordering.ToString)
                lwitem.SubItems.Add(channelList(i).Id.ToString)
                lstChaine.Items.Add(lwitem)
                Math.DivRem(i, 2, reste)
                If (reste = 0) Then
                    lwitem.BackColor = Color.WhiteSmoke
                End If
            Next
        End If
        If lstChaine.Items.Count > 0 Then
            lstChaine.Items(0).Selected = True
        End If
    End Sub

#End Region

    Private Sub btFermer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btFermer.Click
        Me.Close()
    End Sub

    Private Sub PlaylistAndChannelManagement_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Mainform.TimerSoustitresAudio.Enabled = True
        Mainform.TimerSoustitresAudio.Start()
    End Sub

    Private Sub PlaylistAndChannelManagement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Mainform.TimerSoustitresAudio.Stop()
        Mainform.TimerSoustitresAudio.Enabled = False
        rafraichirplaylist()
    End Sub

    Private Sub btRenommerChaine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btRenommerChaine.Click
        If lstChaine.SelectedItems.Count = 1 Then
            txtNomChaines.Text = lstChaine.SelectedItems(0).SubItems(0).Text
            GroupBox4.Visible = True
        End If
    End Sub

    Private Sub btAnnulRenommer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAnnulRenommer.Click
        GroupBox4.Visible = False
    End Sub

    Private Sub btOkRenommer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOkRenommer.Click
        lstChaine.SelectedItems(0).SubItems(0).Text = txtNomChaines.Text
        GroupBox4.Visible = False
    End Sub

    Private Sub btOrange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOrange.Click
        RecupererWebTVOrange()
        rafraichirplaylist()
    End Sub

    Private Sub btScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btScan.Click
        Dim frmfrmscanner As New frmScanner
        Translator.Translate(frmfrmscanner)
        frmfrmscanner.ShowDialog()
        'frmScanner.ShowDialog()
        rafraichirplaylist()
    End Sub
End Class